package com.jketing.expression.parser;

import java.util.Stack;

public class LogicalExpressionParser {
	private Stack<Character> ops = new Stack<Character>();// 运算符栈
	private Stack<String> opds = new Stack<String>();// 操作数栈
	
	private char relation(char left,char right) {
		char[] ch = {left,right};
		int[] index = {-1,-1};
		char[][] re = {
				{'>','>','>','>','<','>','>'},
				{'<','>','>','>','<','>','>'},
				{'<','<','>','>','<','>','>'},
				{'<','<','>','>','<','>','>'},
				{'<','<','<','<','<','=','x'},
				{'>','>','>','>','x','>','>'},
				{'<','<','<','<','<','x','='}
		};
		for(int i=0;i<=1;i++) {
			switch(ch[i]) {
				case'!':index[i]=0;break;
				case'&':index[i]=1;break;
				case'|':index[i]=2;break;
				case'^':index[i]=3;break;
				case'(':index[i]=4;break;
				case')':index[i]=5;break;
				case'#':index[i]=6;break;
				default://出错，抛异常
			}
		}
		return re[index[0]][index[1]];
	}
	
	String queryBuilder (String left,char c,String right) {
		String result = "";
		switch(c) {
		case'!':result += "("+c + ":" + right+")";break;
		case'&':
		case'|':
		case'^':
			result += "("+left + c + right+")";break;
		default://出错，抛异常
		}
		System.out.println("------" + result +"-------");
		return result;
	}
	
	void operate(char ops_peek,char current){
		char temp = relation(ops_peek,current);
		char ops_top;
		switch(temp) {
		case'<':
			ops.push(current);
			break;
		case'=':
			ops_top = ops.pop();
			break;
		case'>':
			ops_top = ops.pop();
			String left,right;
			if(ops_top=='!') {
				right = opds.pop();
				left = "";
			}else {
				right = opds.pop();
				left = opds.pop();
			}
			opds.push(queryBuilder(left,ops_top,right));
			if(current==')') {
				ops.pop();//废去左括号
			}else if(current!='#'){
				operate(ops.peek(),current);//递归比较栈内运算符，直到current<ops.peek入栈为止
			}
			break;
		case'x':
			System.out.println("error");
		}
	}

	/**
	 * isDelimiter 判断一个字符是否为分隔符 
	 * 表达式中的字符包括： 
	 * 与:'!' 或:'｜' 非:'!' 异或:'^' 左小括号:'(' 右小括号:')'
	 */
	private boolean isDelimiter(char c) {
		if (("!|&^()#".indexOf(c) != -1))
			return true;
		return false;
	}
	
	public void parser(String expression){
		ops.push('#');
		int i = 0;
		char c = expression.charAt(i);
		
		while(c != '#'||ops.peek()!='#') {
			if(isDelimiter(c)) {
				operate(ops.peek(),c);
			}else {
				if(c=='[') {
					// 如果当前字符是一个[，则认为是一个查询量开始标记
					// 将解析器指针往后移，知道遇到另一个]分隔符，之间的字符都是查询量的组成部分
					StringBuilder sb = new StringBuilder();
					c = expression.charAt(++i);
					while (c!=']') {
						sb.append(c);
						c = expression.charAt(++i);
					}
					opds.push(sb.toString());
					
				}
			}
			if(++i <  expression.length()) c = expression.charAt(i);
		}
		
		System.out.println("reuslt : " + opds.peek());
	}
	

	public static void main(String[] args) {
		 
		//String expression = "[q1] and ((([q2] or [q3]) and [q4]) or ([q5] or [q6]))";
		//String expression = "[q1] | [q2] &! [q3] ^ [q4]#";
		//String expression = "[q1] & ([q2] | [q3])#";
		String expression = "[q1] | [q2] & [q3] | ([q4] | [q5]) | [q6] & ![q7] ^ [q8]#";
		LogicalExpressionParser t = new LogicalExpressionParser();
		t.parser(expression);		
	}
}
